package DAL;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ClientDAL extends Dal {

	public void insertionClient(int id, String nomFamille, String prenom,
			String courriel, String tel, String anniv, String adresse,
			String ville, String province, String codePostal, String carte,
			String noCarte, int expMois, int expAnnee, String motDePasse,
			String forfait) {
		CallableStatement pstmt = null;
		try {
			pstmt = Dal.connection
					.prepareCall("{call sproc_ajouterClientXML(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
			pstmt.setInt("var_clientID", id);
			pstmt.setString("var_prenom", prenom);
			pstmt.setString("var_nom", nomFamille);

			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

			Date setDate = (Date) df.parse(anniv);
			long dateTime = setDate.getTime();
			java.sql.Date sqlDate = new java.sql.Date(dateTime);
			pstmt.setDate("var_date_Naissance", sqlDate);

			pstmt.setString("var_forfait", forfait);
			pstmt.setString("var_mdp", motDePasse);
			pstmt.setString("var_addresse", adresse);
			pstmt.setString("var_province", province);
			pstmt.setString("var_telephone", tel);
			pstmt.setString("var_courriel", courriel);
			pstmt.setString("var_ville", ville);
			pstmt.setString("var_code_postal", codePostal);
			pstmt.setString("var_ctype", carte);
			pstmt.setString("var_numero", noCarte);
			pstmt.setInt("var_mois_expiration", expMois);
			pstmt.setInt("var_annee_expiration", expAnnee);

			pstmt.registerOutParameter("var_clientID", Types.INTEGER);
			System.out.println("Insertion du client:" + id);
			System.out.println("data: [prenom] " + prenom + " [nom]"
					+ nomFamille + "  [date Naissance]" + anniv + " [forfait]"
					+ forfait + " [mdp]" + motDePasse + " [addr]" + adresse
					+ " [province]" + province + " [tel]" + tel + " [courriel]"
					+ courriel + " [ville]" + ville + " [code postal]"
					+ codePostal + " [TYPE]" + carte + " [card No]" + noCarte
					+ " [mois exp]" + expMois + " [Anne]" + expAnnee);
			pstmt.executeUpdate();
			
			
		} catch (SQLException e) {
			System.out.println("SHITT!!!! HAPPEN");
			e.printStackTrace();
		} catch (ParseException e) {
			// la date d'anniversaire est chier :|
			e.printStackTrace();
		}
		finally{
			try {
				pstmt.close();
			} catch (SQLException e) {
				System.out.println("SHITT!!!! HAPPEN ON THE CLOSE");
				e.printStackTrace();
			}
		}

	}
}
